package com.example.administrator.democ;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;

public class ScoAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Score> scoreArrayList;
    private InOnDelListener listener;

    public ScoAdapter(Context context, ArrayList<Score> scoreArrayList,
                      InOnDelListener listener) {
        this.context = context;
        this.scoreArrayList = scoreArrayList;
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return scoreArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return scoreArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null){
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.sco_item, viewGroup, false);

            ScoViewHolder scoViewHolder = new ScoViewHolder();
            scoViewHolder.txtNo = view.findViewById(R.id.txt_no);
            scoViewHolder.txtAndroid = view.findViewById(R.id.txt_android);
            scoViewHolder.txtEnglish = view.findViewById(R.id.txt_english);
            scoViewHolder.ivDel = view.findViewById(R.id.iv_del);

            view.setTag(scoViewHolder);
        }

        ScoViewHolder viewHolder = (ScoViewHolder) view.getTag();
        final Score s = scoreArrayList.get(i);

        viewHolder.txtNo.setText(s.getStuNo());
        viewHolder.txtAndroid.setText(String.valueOf(s.getAndroid()));
        viewHolder.txtEnglish.setText(String.valueOf(s.getEnglish()));

        viewHolder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.del(s);
            }
        });

        return view;
    }
}
